package com.weimei.conversionTreasure.viewModel;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;

import com.lxj.xpopup.XPopup;
import com.weimei.conversionTreasure.activity.SelKhActivity;
import com.weimei.conversionTreasure.http.Repository;
import com.weimei.conversionTreasure.http.entity.BalanceDetailBean;
import com.weimei.conversionTreasure.http.entity.BaseResult;
import com.weimei.conversionTreasure.http.entity.DealerDetailBean;
import com.weimei.conversionTreasure.utils.UserUtils;
import com.weimei.conversionTreasure.weight.BottomTimePop;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class JyDetailVM extends BaseViewModel<Repository> {
    public JyDetailVM(@NonNull Application application, Repository model) {
        super(application, model);
    }
    Activity mActivity;
    @Override
    public void setActivity(Activity activity) {
        super.setActivity(activity);
        this.mActivity = activity;
    }
    public ObservableField<String> sid = new ObservableField<>();
    public ObservableField<String> amount = new ObservableField<>();
    public ObservableField<String> notes = new ObservableField<>();
    public ObservableField<String> className = new ObservableField<>();
    public ObservableField<String> types = new ObservableField<>();
    public ObservableField<String> customID = new ObservableField<>();
    public ObservableField<String> customName = new ObservableField<>();
    public ObservableField<String> time = new ObservableField<>();


    public ObservableInt showBottom = new ObservableInt(View.GONE);
    public ObservableInt showView = new ObservableInt(View.VISIBLE);
    public ObservableInt showEdit = new ObservableInt(View.VISIBLE);

    public BindingCommand back = new BindingCommand(() -> mActivity.finish());

    public BindingCommand timePick = new BindingCommand(() ->{
        new XPopup.Builder(mActivity)
                .moveUpToKeyboard(false)//如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .asCustom(new BottomTimePop(mActivity, s1 -> {
                    time.set(s1);
                },true,true))
                .show();
    });

    public BindingCommand khPick = new BindingCommand(() ->{
        mActivity.startActivityForResult(new Intent(mActivity, SelKhActivity.class),1);
    });

    public BindingCommand edit = new BindingCommand(() -> {
        showEdit.set(View.INVISIBLE);
        showBottom.set(View.VISIBLE);
        showView.set(View.GONE);
    });

    public BindingCommand sure = new BindingCommand(() -> {
        showEdit.set(View.VISIBLE);
        showBottom.set(View.GONE);
        showView.set(View.VISIBLE);
        editData();
    });

    private void editData() {
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("sid",sid.get());
        map.put("types",types.get());
        map.put("className",className.get());
        map.put("notes",notes.get());
        map.put("amount",amount.get());
        map.put("customID",customID.get());
        model.edit_balance(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.issucc){
                            ToastUtils.showShort(baseResult.msg);
                            finish();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public BindingCommand cancel = new BindingCommand(() -> {
        showEdit.set(View.VISIBLE);
        showBottom.set(View.GONE);
        showView.set(View.VISIBLE);
    });


    public BindingCommand del = new BindingCommand(() -> {
        delData();
    });

    public void getData(String id) {
        sid.set(id);
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("sid",id);
        model.balance_details(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BalanceDetailBean>() {
                    @Override
                    public void onNext(BalanceDetailBean detailBean) {
                        if (detailBean.issucc){
                            amount.set(detailBean.getData().getAmount()+"");
                            notes.set(detailBean.getData().getNotes());
                            className.set(detailBean.getData().getClassName());
                            types.set(detailBean.getData().getTypes());
                            customName.set(detailBean.getData().getCustomName());
                            time.set(detailBean.getData().getDateTime().substring(0,10));
                        }else {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void delData() {
        Map<String,String> map = new HashMap<>();
         map.put("user_id", UserUtils.getUserAccount());
        map.put("sid",sid.get());
        model.delete_balance(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .doOnSubscribe(disposable -> {})
                .subscribe(new DisposableObserver<BaseResult>() {
                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.issucc){
                           ToastUtils.showShort(baseResult.msg);
                           finish();
                        }else {
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
